<template>
  <el-card class="downloads-container">
    <template #header>
      <h3>我的下载记录</h3>
    </template>
    
    <el-table
      :data="downloads"
      style="width: 100%"
      v-loading="loading"
    >
      <el-table-column prop="material.title" label="资料名称" />
      <el-table-column prop="createdAt" label="下载时间" width="180" />
      <el-table-column prop="coinsSpent" label="消耗虚拟币" width="120" />
    </el-table>
    
    <div class="pagination">
      <el-pagination
        v-model:current-page="pagination.current"
        v-model:page-size="pagination.size"
        :total="pagination.total"
        @current-change="handlePageChange"
        layout="total, prev, pager, next"
      />
    </div>
  </el-card>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { getDownloadHistory } from '@/api/user'

const downloads = ref([])
const loading = ref(false)
const pagination = ref({
  current: 1,
  size: 10,
  total: 0
})

const loadData = async () => {
  try {
    loading.value = true
    const res = await getDownloadHistory({
      page: pagination.value.current,
      size: pagination.value.size
    })
    downloads.value = res.data.items
    pagination.value.total = res.data.total
  } catch (error) {
    console.error('获取下载记录失败:', error)
  } finally {
    loading.value = false
  }
}

const handlePageChange = () => {
  loadData()
}

onMounted(() => {
  loadData()
})
</script>

<style scoped>
.downloads-container {
  min-height: 500px;
}

.pagination {
  margin-top: 20px;
  text-align: right;
}
</style>